package com.gw.business.system.user.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gw.business.common.DwzAjaxRes;
import com.gw.business.system.role.model.UserRoleModel;
import com.gw.platform.controller.DefaultPageController;
import com.gw.platform.model.UserModel;
import com.gw.platform.page.impl.Page;
import com.gw.platform.util.StringUtils;

/**
 * 
 * 类功能:处理用户登录请求控制器
 * 
 *  
 * <p>创建者:关博</p>
 * 
 * <p>创建时间:2014-06-20</p>
 * 
 * <p>修改者:</p>
 * 
 * <p>修改时间:</p>
 * 
 * <p>修改原因：</p>
 * 
 * <p>审核者:</p>
 * 
 * <p>审核时间:</p>
 * 
 * <p>审核意见：</p>
 * 
 */
@Controller
@RequestMapping("sysuser")
public class SystemUserController extends DefaultPageController{
	
	/**
	 * 分页查询所有信息
	 * 
	 * @return
	 */
	@RequestMapping("/page")
	public Callable<String> page() {
		return new Callable<String>() {
			public String call() throws Exception {
				String pageSize = request.getParameter("numPerPage");
				String pageNum = request.getParameter("pageNum");

				if(StringUtils.isEmpty(pageNum)){
					pageNum = "1";
					pageSize = Page.DEFAULT_PAGE_SIZE + "";;
				}
				// 设置当前页
				setCurrent(Long.parseLong(pageNum));
				String systemUserName = request.getParameter("systemUserName");
				Map<String, Object> map = new HashMap<String, Object>();
				if(!"".equals(systemUserName)){
					map.put("userName", systemUserName);
				}
				
				page = defaultService.page("sysUser.user_page", "sysUser.user_total",
						getCurrent(), map,Integer.parseInt(pageSize));
				// 设置分页操作的url
				request.setAttribute("page", page);
				request.setAttribute("systemUserName", systemUserName);
				return "system/sysuser/page";
			}
		};
	}
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create() {
		request.setAttribute("roleList", defaultService.find("role.role_list"));
		return "system/sysuser/create";
	}
	
	/**
	 * 跳转到加载用户页面
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public Callable<String> update() {
		return new Callable<String>() {
			public String call() throws Exception {
				UserModel  user = new UserModel();
				user.setId(Integer.parseInt(request.getParameter("uId")));
				
				request.setAttribute("userModel", defaultService.load("sysUser.user_info",user));
				request.setAttribute("roleList", defaultService.find("role.role_list"));
				
				return "system/sysuser/update";
			}
		};
	}
	
	/**
	 * 添加系统用户
	 * @return
	 */
	@RequestMapping("/updateSave")
	@ResponseBody
	public Callable<Object> updateSave(final UserModel user) {
		return new Callable<Object>() {
			public Object call() throws Exception {
				String callbackType = request.getParameter("callbackType");
				String navTabId = request.getParameter("navTabId");
				
				DwzAjaxRes res = new DwzAjaxRes();
				res.setCallbackType(callbackType);
				res.setNavTabId(navTabId);
				Map map= (Map) defaultService.load("sysUser.user_loadByName",user);
				if(map != null){
					res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
					res.setMessage(user.getUserName() + "帐号已经存在！");
				}else{
					defaultService.update("sysUser.user_update", user);
					
					UserRoleModel userRoleModel = new UserRoleModel();
					userRoleModel.setRoleId(Integer.parseInt(request.getParameter("roleId")));
					userRoleModel.setUserId(user.getId());
					defaultService.update("userRole.userRole_update", userRoleModel);
				}
				return res;
			}
		};
	}
	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Callable<Object> delete(final UserModel user) {
		return new Callable<Object>() {
			public Object call() throws Exception {
				String callbackType = request.getParameter("callbackType");
				String navTabId = request.getParameter("navTabId");
				
				DwzAjaxRes res = new DwzAjaxRes();
				res.setCallbackType(callbackType);
				res.setNavTabId(navTabId);
				defaultService.delete("sysUser.user_delete",Integer.parseInt(request.getParameter("uId")));
				
				UserRoleModel userRoleModel = new UserRoleModel();
				userRoleModel.setUserId(Integer.parseInt(request.getParameter("uId")));
				defaultService.delete("userRole.userRole_delete", userRoleModel);
				
				return res;
			}
		};
	}
	
	/**
	 * 添加系统用户
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Callable<Object> insert(final UserModel user) {
		return new Callable<Object>() {
			public Object call() throws Exception {
				String callbackType = request.getParameter("callbackType");
				String navTabId = request.getParameter("navTabId");
				
				DwzAjaxRes res = new DwzAjaxRes();
				res.setCallbackType(callbackType);
				res.setNavTabId(navTabId);
				Map map= (Map) defaultService.load("sysUser.user_loadByName",user);
				if(map != null){
					res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
					res.setMessage(user.getUserName() + "帐号已经存在！");
				}else{
					defaultService.insert("sysUser.user_inset", user);
					
					UserRoleModel userRoleModel = new UserRoleModel();
					userRoleModel.setRoleId(Integer.parseInt(request.getParameter("roleId")));
					userRoleModel.setUserId(user.getId());
					defaultService.insert("userRole.userRole_insert", userRoleModel);
				}
				
				return res;
			}
		};
	}
}

